/*
 *  MyTransportArray.h
 *  JuceAudioTemplateApp
 *
 *  Created by Matthew Pott on 22/10/2008.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */
#ifndef _MYTRANSPORTSOURCE_H_
#define _MYTRANSPORTSOURCE_H_

class MyTransportSource : public AudioTransportSource
	
	{
	private:
		
		AudioFormatReaderSource* currentAudioFileSource;

		
	public:
		
		MyTransportSource( )
		{
			currentAudioFileSource = 0;						
		}
		
		MyTransportSource(const String& path)
		{
			currentAudioFileSource = 0;						
			plugAudioFileIntoTransport(path);
		}
		
		
		~MyTransportSource()
		{
			setSource(0);
			deleteAndZero (currentAudioFileSource);
		}
		
		bool plugAudioFileIntoTransport(const String& path)
		{
			// make an appropriate "reader" for the type of file we have (again this is defined below)
			AudioFormatReader* reader = audioFormatReaderFromFile(path);
			
			if (reader != 0)
			{			
				// reader wasn't zero so the operation was successful
				// now turn that into and audio source that can read data into our transport
				currentAudioFileSource = new AudioFormatReaderSource (reader, false);
				
				// ..and plug it into our transport source
				setSource (currentAudioFileSource,
									 32768, // tells it to buffer this many samples ahead
									 reader->sampleRate);
				
				// success
				return true;
			}
			
			// failed
			return false;
		}
		
		AudioFormatReader* audioFormatReaderFromFile(const String& path)
		{
			// creat a file object from or path
			File audioFile (path);
			
			// get a format manager and set it up with the basic types (i.e., wav and aiff).
			AudioFormatManager formatManager;
			formatManager.registerBasicFormats();
			
			return formatManager.createReaderFor (audioFile);
		}
		
	};		
		
#endif//_MYTRANSPORTSOURCE_H_
